/**
 *  温度统计
 *  Created by lihao on 2018-04-24
 */

let express = require('express');
let router = express.Router();

let db = require('../conf');
let Tools = require('../util');

/* GET users listing. */
router.get('/', function(req, res, next) {
    res.render('temp',{title: 'temp'})
});

/* 更新温度 */
router.post('/update', function(req, res, next) {
    let sql = 'INSERT INTO temp VALUES (NULL,NOW(),?)';

    let a = req.body;

    db(sql,[a.t]).then( b => {
        // _bySocket.on('update',function(){
        //     socket.broadcast.emit('number', { number: num.length });
        // });

        // _bySocket.broadcast.emit('tempUpdate', { temp: 23.4 });

       // _bySocketIo.emit('tempUpdate',{ temp: 23.4 });

        /**
         *  将更新后的所有数据返回给前端
         */

        let sqlAll = 'SELECT * FROM temp';

        db(sqlAll,[]).then( b => {
            let dataTemp = [];
            for(let i =0; i< b.length; i++){
                let c = [];
                c.push(Tools.parseTime(b[i].currentTime));
                c.push(b[i].temp);
                dataTemp.push(c)
            }

            _bySocketIo.emit('tempUpdateDemo',{ temp: dataTemp });

        } );

        res.send({
            success: false,
            message: '保存温度成功',
            code: 0,
            data: {
                res: b
            }

        })
    }).catch( err => {
        res.send({
            success: false,
            message: '保存温度失败',
            code: 0,
            data: {
                error: err
            }

        })
    })
});


/* 更新温度 */
router.get('/update', function(req, res, next) {
    let sql = 'INSERT INTO temp VALUES (NULL,NOW(),?)';

    let a = req.query;

    db(sql,[a.t]).then( b => {
        // _bySocket.on('update',function(){
        //     socket.broadcast.emit('number', { number: num.length });
        // });

        // _bySocket.broadcast.emit('tempUpdate', { temp: 23.4 });

       // _bySocketIo.emit('tempUpdate',{ temp: 23.4 });

        /**
         *  将更新后的所有数据返回给前端
         */

        let sqlAll = 'SELECT * FROM temp';

        db(sqlAll,[]).then( b => {
            let dataTemp = [];
            for(let i =0; i< b.length; i++){
                let c = [];
                c.push(Tools.parseTime(b[i].currentTime));
                c.push(b[i].temp);
                dataTemp.push(c)
            }

            _bySocketIo.emit('tempUpdateDemo',{ temp: dataTemp });

        } );

        res.send({
            success: false,
            message: '保存温度成功',
            code: 0,
            data: {
                res: b
            }

        })
    }).catch( err => {
        res.send({
            success: false,
            message: '保存温度失败',
            code: 0,
            data: {
                error: err
            }

        })
    })
});

// 获取温度列表
router.post('/getTemp', function(req, res, next) {
    let sql = 'SELECT * FROM temp';

    db(sql,[]).then( b => {

        let dataTemp = [];
        for(let i =0; i< b.length; i++){
            let c = [];
            c.push(Tools.parseTime(b[i].currentTime));
            c.push(b[i].temp);
            dataTemp.push(c)
        }

        res.send({
            success: 'true',
            message: '成功获取温度列表',
            code: 0,
            data: dataTemp
        })
    } )
});

module.exports = router;